﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.Data.Linq;
using winproject.Library;

namespace winproject.Models
{
    public class MCategory
    {
        DBFunction oDBConf = null;

        public ReturnResult AddNewCategory(Category newCat)
        {
            ReturnResult oResult = null;
            try
            {
                Category ncat= new Category();
                ncat.Cat_Name=newCat.Cat_Name;
                ncat.Cat_Slug=newCat.Cat_Slug;
                ncat.Cat_Description=newCat.Cat_Description;
                ncat.Status=1;

                //oDBConf = new DBFunction();
                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                if (oWinContext.DatabaseExists())
                {
                    oWinContext.Categories.InsertOnSubmit(ncat);
                    oWinContext.SubmitChanges();
                    if (ncat.ID > 0)
                    {
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = JsonConvert.SerializeObject(ncat) };
                    }
                    else{
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.RecordNotInserted, msgJson = JsonConvert.SerializeObject(ncat) };
                    }   
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }

        public ReturnResult getCategoryDetails(Category searchCat)
        {
            ReturnResult oResult = null;
            try
            {

                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                if (oWinContext.DatabaseExists())
                {
                    //var lsCategories = (from c in oWinContext.Categories where (c.Status == (int)statusId) select new { c.ID, c.Cat_Name, c.Cat_Description, c.Cat_Slug, c.Status }) as List<Category>;
                    var lsCategories = oWinContext.Categories.Where(c => c.Cat_Slug == searchCat.Cat_Slug).Select(c => new { c.ID, c.Cat_Name, c.Cat_Description, c.Cat_Slug, c.Status });
                    //var lsCategories = qCatList.ToList<Category>();
                    string catJson = JsonConvert.SerializeObject(lsCategories);
                    if (lsCategories != null)
                    {
                        if (!string.IsNullOrEmpty(catJson))
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = catJson };
                        }
                    }
                    else
                    {
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = catJson };
                    }
                }
            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
        public ReturnResult getActiveCategoriesJSON(cEnums.CategoryStatus statusId)
        {
            ReturnResult oResult = null;
            try
            {

                var oWinContext = new WinProductsDataContext(Convert.ToString(HttpContext.Current.Application["constr"]));
                if (oWinContext.DatabaseExists())
                {
                    //var lsCategories = (from c in oWinContext.Categories where (c.Status == (int)statusId) select new { c.ID, c.Cat_Name, c.Cat_Description, c.Cat_Slug, c.Status }) as List<Category>;
                    var lsCategories = oWinContext.Categories.Where(c => c.Status == Convert.ToInt16(statusId)).Select(c => new { c.ID, c.Cat_Name, c.Cat_Description, c.Cat_Slug, c.Status });
                    //var lsCategories = qCatList.ToList<Category>();
                    string catJson = JsonConvert.SerializeObject(lsCategories);
                    if (lsCategories!=null)
                    {
                        if (!string.IsNullOrEmpty(catJson))
                        {
                            oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Success, msgJson = catJson };
                        }
                    }
                    else
                    {
                        oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.NoRecordsFound, msgJson = catJson };
                    }

                }



            }
            catch (Exception ex)
            {
                oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.Exception, msgJson = string.Format("Exception: {0}", ex.ToString()) };
            }
            return oResult;

        }
    }
}